package com.lancoo.answer.widget.subject

import android.content.Context
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ivan.easycamera.util.ScreenUtils
import com.lancoo.answer.R
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.BottomPopupView

class SubjectSelectHelper private constructor(context: Context) : BottomPopupView(context){
    private lateinit var builder: Builder

    override fun getImplLayoutId(): Int {
        return R.layout.ev_custom_bottom_multi_subject_select
    }

    constructor( builder: Builder):this(builder.context){
        this.builder = builder
    }

    override fun onCreate() {
        super.onCreate()
        val tv_add = findViewById<TextView>(R.id.tv_add)
        val tv_cancel = findViewById<TextView>(R.id.tv_cancel)
        val tv_title = findViewById<TextView>(R.id.tv_title)
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView_select)
        val rl_top = findViewById<ViewGroup>(R.id.rl_top)

        rl_top.visibility = if (builder.isShowTopOperate) VISIBLE else GONE
        tv_add.text = builder.confirm
        tv_cancel.text = builder.cancle
        tv_title.text = builder.title

        val layoutManager = LinearLayoutManager(context)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = builder.adapter
        builder.adapter.setOnItemClickListener { adapter, view, position ->
            builder.adapter.setCurrentSelect(position)
            if (!builder.isShowTopOperate){
                builder.iSelectEventListener?.onConfirm(builder.adapter.currentSelect)
                dismiss()
            }
        }

        tv_add.setOnClickListener {
            builder.iSelectEventListener?.onConfirm(builder.adapter.currentSelect)
            dismiss()
        }
        tv_cancel.setOnClickListener {
            builder.iSelectEventListener?.onCancle()
            dismiss()
        }

    }


    override fun onDismiss() {
        super.onDismiss()
        builder.iSelectEventListener?.onDimisss()
    }


    open class Builder constructor(val context: Context){
        var isShowTopOperate:Boolean = true
        var title:String = "筛选"
        var confirm:String = "确定"
        var cancle:String = "取消"
        var iSelectEventListener: ISelectSubjectListener<*>? = null
        lateinit var selectData:MutableList<*>
        lateinit var adapter: SubjectSelectAdapter<*>

        fun isShowTop(isShowTop:Boolean): Builder {
            this.isShowTopOperate = isShowTop
            return this
        }

        fun title(title:String): Builder {
            this.title = title
            return this
        }

        fun confirm(confirm:String): Builder {
            this.confirm = confirm
            return this
        }

        fun cancle(cancle:String): Builder {
            this.cancle = cancle
            return this
        }

        fun selectData(selectData:MutableList<*>): Builder {
            this.selectData = selectData
            return this
        }

        fun<T> adapter(selectData:MutableList<T>,adapter: SubjectSelectAdapter<T>): Builder {
            this.adapter = adapter
            this.selectData = selectData
            adapter.addData(selectData)
            return this
        }

        fun<T> listener(iSelectEventListener: ISelectSubjectListener<T>) : Builder {
            this.iSelectEventListener = iSelectEventListener
            return this
        }

        fun show() :BasePopupView{
            return SubjectSelectHelper(this).showDialog()
        }
    }

    fun showDialog(): BasePopupView {

        XPopup.Builder()
            .enableDrag(true)
            .moveUpToKeyboard(false)
            .isDestroyOnDismiss(true)
            .dismissOnTouchOutside(true)
            .asCustom(this)
            .show()
        return this
    }


    override fun getPopupHeight(): Int {
        return (ScreenUtils.getScreenHeight(context) * .35f).toInt()
    }


}